/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.shop.web.shop;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.CacheUtils;
import com.thinkgem.jeesite.common.utils.DateUtils;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.utils.excel.ExportExcel;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.act.utils.ExpressUtil;
import com.thinkgem.jeesite.modules.hk.shop.entity.order.HkShopUserOrder;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopExpress;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkShopUserOrderWithdraw;
import com.thinkgem.jeesite.modules.hk.shop.entity.shop.HkUserShop;
import com.thinkgem.jeesite.modules.hk.shop.service.order.HkShopExpressService;
import com.thinkgem.jeesite.modules.hk.shop.service.shop.HkShopUserOrderWithrawService;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

/**
 * 用户订单管理Controller
 *
 * @author 罗天文
 * @version 2018-08-22
 */
@Controller
@RequestMapping(value = "${adminPath}/shop/shop/hkShopUserOrderWithraw")
public class HkShopUserOrderWithrawController extends BaseController {

    private static final String EXPRESS_CACHE = "expressCache";

    @Autowired
    private HkShopUserOrderWithrawService hkShopUserOrderWithrawService;
    @Autowired
    private HkShopExpressService hkShopExpressService;

    @ModelAttribute
    public HkShopUserOrderWithdraw get(@RequestParam(required = false) String id) {
        HkShopUserOrderWithdraw entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = hkShopUserOrderWithrawService.get(id);
        }
        if (entity == null) {
            entity = new HkShopUserOrderWithdraw();
        }
        return entity;
    }

    @RequiresPermissions("shop:shop:hkShopUserOrderWithraw:view")
    @RequestMapping(value = {"list", ""})
    public String list(HkShopUserOrderWithdraw hkShopUserOrderWithdraw, HttpServletRequest request, HttpServletResponse response, Model model) {
        HkUserShop hkUserShop = new HkUserShop();
        hkUserShop.setId(UserUtils.getShopId());
        hkShopUserOrderWithdraw.setShop(hkUserShop);
        Page<HkShopUserOrderWithdraw> page = hkShopUserOrderWithrawService.findPage(new Page<HkShopUserOrderWithdraw>(request, response), hkShopUserOrderWithdraw);
        model.addAttribute("page", page);
        return "hk/shop/shop/hkShopUserOrderWithrawList";
    }

    @RequiresPermissions("shop:shop:hkShopUserOrderWithraw:view")
    @RequestMapping(value = "form")
    public String form(HkShopUserOrder hkShopUserOrder, Model model) {
        model.addAttribute("hkShopUserOrder", hkShopUserOrder);
        return "hk/shop/shop/hkShopUserOrderWithrawForm";
    }


    /**
     * 导出订单数据
     *
     * @param hkShopUserOrderWithdraw
     * @param request
     * @param response
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("shop:shop:hkShopUserOrderWithraw:view")
    @RequestMapping(value = "export", method = RequestMethod.POST)
    public String exportFile(HkShopUserOrderWithdraw hkShopUserOrderWithdraw, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try {
            String fileName = "用户订单提现数据" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            Page<HkShopUserOrderWithdraw> page = hkShopUserOrderWithrawService.findCoupon(new Page<HkShopUserOrderWithdraw>(request, response, -1), hkShopUserOrderWithdraw);
            new ExportExcel("用户订单提现数据", HkShopUserOrder.class).setDataList(page.getList()).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导出用户订单提现失败失败：" + e.getMessage());
        }
        return "redirect:" + adminPath + "/shop/shop/hkShopCoupon/list?repage";
    }


    /**
     * 获取快递公司
     *
     * @param request
     * @param response
     * @param model
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("shop:shop:hkShopUserOrderWithraw:view")
    @RequestMapping(value = "getExpressCompay")
    @ResponseBody
    public void getExpressCompay(HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes) {
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");
        try {
            List<HkShopExpress> expressList = hkShopExpressService.getExpressCompany();
            JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(expressList));
            out = response.getWriter();
            out.print(jsonArray);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
    }


    /**
     * 获取快递信息
     *
     * @param request
     * @param response
     * @param model
     * @param redirectAttributes
     * @return
     */
    @RequiresPermissions("shop:shop:hkShopUserOrderWithraw:view")
    @RequestMapping(value = "getExpress")
    @ResponseBody
    public String getExpress(HttpServletRequest request, HttpServletResponse response, Model model, RedirectAttributes redirectAttributes) {
        PrintWriter out = null;
        response.setCharacterEncoding("utf-8");

        String orderNumber = request.getParameter("orderNumber");
        String expressCompany = request.getParameter("express");
//        String orderNumber = "260034815902";
//        String expressCompany = "zhongtong";
        try {

            Object jsonData = CacheUtils.get(EXPRESS_CACHE, orderNumber.trim() + expressCompany.trim());
            if (jsonData == null || jsonData.equals("")) {
                jsonData = ExpressUtil.getExpress(expressCompany, orderNumber);
                CacheUtils.put(EXPRESS_CACHE, orderNumber.trim() + expressCompany.trim(), jsonData);
            }
            out = response.getWriter();
            out.print(jsonData);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
        }
        return null;
    }

    /**
     * 修改体现状态
     *
     * @return
     */
    @RequiresPermissions("shop:shop:hkShopUserOrderWithraw:edit")
    @RequestMapping(value = "updateWithrawByOrderNumber")
    public String updateWithrawByOrderNumber(HttpServletRequest request, RedirectAttributes redirectAttributes) {

        if (StringUtils.isBlank(UserUtils.getShopId())) {
            if (StringUtils.isBlank(request.getParameter("orderNumber"))) {
                addMessage(redirectAttributes, "登录超时，请重新登录。");
                return "redirect:" + Global.getAdminPath();
            }
        }

        if (StringUtils.isBlank(request.getParameter("orderNumber"))) {
            addMessage(redirectAttributes, "订单不存在");
            return "redirect:" + adminPath + "/shop/shop/hkShopUserOrderWithraw/list?repage";
        }

        String ordreNumber = request.getParameter("orderNumber");
        hkShopUserOrderWithrawService.updateWithrawByOrderNumber(ordreNumber);
        addMessage(redirectAttributes, "申请提现成功");
        return "redirect:" + adminPath + "/shop/shop/hkShopUserOrderWithraw/list?repage";
    }


}